package com.liuya.page;

import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页信息记录Bean，存储分页查询需要的信息
 * @Author 刘亚
 * @Date 2016/12/12
 */
public class Pageable<T> extends PageInfo {

    private static final long serialVersionUID = -3930180379790344299L;
    private String orderProperty;
    private String orderDirection;
    private List<Filter> filters = new ArrayList<Filter>();

    public Pageable() {
    }

    public Pageable(List<T> list) {
        super(list);
    }

    public Pageable(Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageNum.intValue() >= 1)
            super.setPageNum(pageNum.intValue());
        if (pageSize != null && pageSize.intValue() >= 1 && pageSize.intValue() <= 1000)
            super.setPageSize(pageSize.intValue());
    }

    public void setPageNum(int pageNum) {
        if (pageNum < 1)
            pageNum = 1;
        super.setPageNum(pageNum);
    }

    public void setPageSize(int pageSize) {
        if (pageSize < 1 || pageSize > 1000)
            pageSize = 20;
        super.setPageSize(pageSize);
    }

    public String getOrderProperty() {
        return this.orderProperty;
    }

    public void setOrderProperty(String orderProperty) {
        this.orderProperty = orderProperty;
    }

    public String getOrderDirection() {
        return this.orderDirection;
    }

    public void setOrderDirection(String orderDirection) {
        this.orderDirection = orderDirection;
    }

    public List<Filter> getFilters() {
        return this.filters;
    }

    public void setFilters(List<Filter> filters) {
        this.filters = filters;
    }

    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        if (this == obj)
            return true;
        Pageable pageable = (Pageable) obj;
        return new EqualsBuilder()
                .append(getPageNum(), pageable.getPageNum())
                .append(getPageSize(), pageable.getPageSize())
                .append(getOrderProperty(), pageable.getOrderProperty())
                .append(getOrderDirection(), pageable.getOrderDirection())
                .append(getFilters(), pageable.getFilters()).isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder(17, 37).append(getPageNum())
                .append(getPageSize()).append(getOrderProperty())
                .append(getOrderDirection()).append(getFilters())
                .toHashCode();
    }

}
